﻿using System;
using System.Globalization;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.SystemManage;
using Ninject;
using WinForm.Helper;
using WinForm.Properties;

namespace WinForm.Common
{
    public partial class EditSRole : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ISysRole _sysRole = Kernel.Get<ISysRole>();

        public EditSRole()
        {
            InitializeComponent();
        }

        public EditSRole(int id)
        {
            InitializeComponent();
            if (id > 0)
            {
                lbRoleId.Text = id.ToString(CultureInfo.InvariantCulture);
                Text = "编辑角色";
                var r = _sysRole.GetRole(id);
                if (r != null)
                {
                    txtRoleName.Text = r.Obj.Rolename;
                    lbRoleId.Text = r.Obj.Roleid.ToString(CultureInfo.InvariantCulture);
                }
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                var role = new Role
                {
                    Roleid = Converts.ConvertToInt(lbRoleId.Text),
                    Rolename = this.txtRoleName.Text
                };

                ResultInfo<int> id = _sysRole.CreateUpdateRole(role);
                if (id.ResultNumber == ResultEnum.Success)
                {
                    MyMessageBox.ShowMessageBoxInfo(Resources.Imformation_DoSuccess);

                    DialogResult = DialogResult.OK;

                }
                else
                {
                    MyMessageBox.ShowMessageBoxInfo(id.Describe);

                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo(Resources.Imformation_SaveFailed);

            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}
